<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '#' }">
                运动会系统信息管理
            </el-breadcrumb-item>
            <el-breadcrumb-item>
                <a href="#">用户信息管理</a>
            </el-breadcrumb-item>
        </el-breadcrumb>

        <el-card class="user-card">
            <el-row class="user-select">
                <el-col class="col1" :span="2">
                    <el-button type="warning" @click="adddialogVisible = true">添加</el-button>
                </el-col>
            </el-row>

            <el-table :data="currentPageData" border stripe>
                <el-table-column type="index" label="No" width="60">
                </el-table-column>
                <el-table-column prop="username" label="用户">
                </el-table-column>
                <el-table-column prop="userrealname" label="姓名/团名">
                </el-table-column>
                <el-table-column prop="userstatus" label="用户身份">
                </el-table-column>
                <el-table-column prop="pagescale" label="项数/团ID">
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button type="primary" @click="showeditdialog(scope.row)">修改</el-button>
                        <el-button type="danger" @click="userdelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination class="paging" @size-change="handleSizeChange" @current-change="handlePageChange" background
                :page-sizes="[3, 5, 10, 15, 20, 50]" layout="total, prev, pager, next, jumper" :total="total"
                :page-size="queryObj.limit" :current-page="queryObj.page">
            </el-pagination>

            <!-- 添加页面 -->
            <el-dialog title="添加" v-model="adddialogVisible" draggable width="40%" :before-close="addleClose">
                <el-form :model="addfrom" label-width="120" :rules="rules" ref="addForm">
                    <el-form-item prop="username" label="用户：">
                        <el-input v-model="addfrom.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="userpassword" label="密码：">
                        <el-input v-model="addfrom.userpassword" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="板限：">
                        <el-select v-model="addfrom.userlevel">
                            <el-option label=" " :value="0"></el-option>
                            <el-option label="管理人" :value="1"></el-option>
                            <el-option label="用人" :value="2"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="userrealname" label="团名：">
                        <el-input v-model="addfrom.userrealname"></el-input>
                    </el-form-item>
                    <el-form-item label="团ID：">
                        <el-input v-model="addfrom.pagescale"></el-input>
                    </el-form-item>
                    <el-form-item label="身份证：">
                        <el-input v-model="addfrom.userstatus"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="addleClose">取 消</el-button>
                    <el-button type="primary" @click="adduser">确 定</el-button>
                </div>
            </el-dialog>

            <!-- 修改页面 -->
            <el-dialog title="修改" v-model="editdialogVisible" draggable width="40%" :before-close="handleClose">
                <el-form :model="editfrom" label-width="120">
                    <el-form-item label="用户：">
                        <el-input v-model="editfrom.username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码：">
                        <el-input v-model="editfrom.userpassword"></el-input>
                    </el-form-item>
                    <el-form-item label="团名：">
                        <el-input v-model="editfrom.userrealname"></el-input>
                    </el-form-item>
                    <el-form-item label="团ID：">
                        <el-input v-model="editfrom.pagescale"></el-input>
                    </el-form-item>
                    <el-form-item label="身份证：">
                        <el-input v-model="editfrom.userstatus"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancelEdit">取 消</el-button>
                    <el-button type="primary" @click="editUserinfo">确 定</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
export default {
    data() {
        return {
            queryObj: {
                limit: 10,
                page: 1,
                query: " "
            },
            total: 0,
            userData: [],
            currentPageData: [],
            editdialogVisible: false,
            editfrom: {},
            adddialogVisible: false,
            addfrom: {
                userid: null,
                userpassword: null,
                userlevel: null,
                userrealname: '',
                pagescale: '',
                userstatus: '',
                username: '',
            },
            initialForm: {},
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                ],
                userpassword: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                ],
                userrealname: [
                    { required: true, message: '请输入团名', trigger: 'blur' },
                ],
            }
        }
    },
    created() {
        this.initialForm = { ...this.addfrom }
        this.getUserinfoList()
    },
    methods: {
        async getUserinfoList() { // 数据
            try {
                const result = await this.$http.post(this.BACKEND_URL + '/user/getmsg');
                if (result.data.code == 0) {
                    this.userData = result.data.data
                    this.total = this.userData.length
                    this.fetchData()
                } else if (result.data.code == 500) {
                    console.log(result.data.msg)
                }
            } catch (error) {
                if (error === undefined) {
                    this.$message.error('连接错误-获取数据失败');
                }
            }
        },
        // 添加
        async adduser() {
            this.$refs.addForm.validate(async (valid,fsda) => {
                if (valid) {
                    const result = await this.$http.post(this.BACKEND_URL + '/user/insertmsg', this.addfrom)
                    if (result.data.code == 0) {
                        this.adddialogVisible = false
                        this.getUserinfoList();
                        this.$message.success('添加成功')
                        this.addfrom = { ...this.initialForm }
                    } else if (result.data.code == 500) {
                        this.addfrom = { ...this.initialForm }
                        this.$message.error(result.data.msg)
                    }
                } else {
                    this.$message.error('请填写必要信息');
                }
            });
        },
        addleClose() {
            this.adddialogVisible = false
            this.addfrom = { ...this.initialForm }
        },
        // 删除单条数据
        userdelete(e) {
            this.$confirm('此操作将删除改条数据，是否继续', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(async () => {
                const result = await this.$http.post(this.BACKEND_URL + '/user/deletemsg', e)
                if (result.data.code === 0) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getUserinfoList()
                } else if (result.data.code == 500) {
                    this.$message.error(result.data.msg)
                }
            }).catch(() => {
            });
        },
        // 获取单个数据信息
        async showeditdialog(info) {
            // console.log(info);
            this.editdialogVisible = true
            this.editfrom = info
        },
        async editUserinfo() {
            const result = await this.$http.post(this.BACKEND_URL + '/user/updatemsg', this.editfrom)
            if (result.data.code == 0) {
                this.editdialogVisible = false
                this.getUserinfoList();
                this.$message.success('修改成功')
            } else if (result.data.code == 500) {
                this.getUserinfoList();
                this.$message.error(result.data.msg)
            }
        },
        cancelEdit() {
            this.editdialogVisible = false
            this.getUserinfoList()
        },
        handleClose(done) {
            this.getUserinfoList()
            done()
        },
        // 分页
        fetchData() {
            let startIndex = (this.queryObj.page - 1) * this.queryObj.limit;
            let endIndex = startIndex + this.queryObj.limit;
            this.currentPageData = this.userData.slice(startIndex, endIndex);
        },
        handleSizeChange(newSize) {
            this.queryObj.limit = newSize
        },
        handlePageChange(newPage) {
            this.queryObj.page = newPage;
            let startIndex = (newPage - 1) * this.queryObj.limit;
            let endIndex = startIndex + this.queryObj.limit;
            this.currentPageData = this.userData.slice(startIndex, endIndex);
        }
    }
}
</script>

<style lang="less" scoped>
.user-card {
    margin-top: 15px;

    .col1 {
        text-align: left;
    }

    .user_top {
        text-align: right;

        a {
            text-decoration: none;
            margin-left: 15px;
        }
    }

    .el-table {
        margin-top: 15px;
    }
}

.paging {
    margin-top: 15px;
}
</style>